package com.learn.grpc.interceptor;

import io.grpc.*;

/**
 * @author zongf
 * @since 2023-07-06
 **/
public class MyServerRequestInterceptor implements ServerInterceptor {
    @Override
    public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> serverCall, Metadata metadata,
                                                                 ServerCallHandler<ReqT, RespT> next) {


        return new ForwardingServerCallListener.SimpleForwardingServerCallListener<ReqT>(next.startCall(serverCall, metadata)) {
            @Override
            public void onMessage(ReqT message) {
                metadata.put(Metadata.Key.of("start", Metadata.ASCII_STRING_MARSHALLER), String.valueOf(System.currentTimeMillis()));

                super.onMessage(message);
                System.out.println("【MyServer2Interceptor】接收到消息：" + message);
            }

        };
    }
}
